package com.sskj.community.ui.fragment;

import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.sskj.common.adapter.MyFragmentPagerAdapter;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.community.R;
import com.sskj.community.R2;
import com.sskj.community.presenter.CommunityPresenter;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseFragment;
import com.sskj.lib.bean.TabEntity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * @author Hey
 * Create at  2019/06/25
 */
@Route(path = RConfig.COMMUNITY_FRAGMENT_COMMUNITY)
public class CommunityFragment extends BaseFragment<CommunityPresenter> {

    @BindView(R2.id.tvTitle)
    TextView tvTitle;
    @BindView(R2.id.tvRight)
    TextView tvRight;
    @BindView(R2.id.coin_tab)
    CommonTabLayout coinTab;
    @BindView(R2.id.viewPager)
    ViewPager viewPager;
    private String[] mTitles = {App.INSTANCE.getString(R.string.community_communityFragment1), App.INSTANCE.getString(R.string.community_communityFragment2)};
    private List<Fragment> fragmentList;

    @Override
    public int getLayoutId() {
        return R.layout.community_fragment_community;
    }

    @Override
    public CommunityPresenter getPresenter() {
        return new CommunityPresenter();
    }

    @Override
    public void initView() {
        tvTitle.setText(App.INSTANCE.getString(R.string.community_communityFragment3));

        ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i]));
        }
        coinTab.setTabData(mTabEntities);
        fragmentList = new ArrayList<>();
        fragmentList.add((Fragment) ARouter.getInstance().build(RConfig.COMMUNITY_FRAGMENT_HOT).navigation());
        fragmentList.add((Fragment) ARouter.getInstance().build(RConfig.COMMUNITY_FRAGMENT_PUBLISH).navigation());
        coinTab.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                if (position == 1) {
                    tvRight.setVisibility(View.GONE);
                } else {
                    tvRight.setVisibility(View.VISIBLE);
                }
                viewPager.setCurrentItem(position, true);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
        viewPager.setAdapter(new MyFragmentPagerAdapter(getChildFragmentManager(), fragmentList));
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                coinTab.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public void initData() {
        ClickUtil.click(300, tvRight, () -> {
            if (checkCetification()) {
                ARouter.getInstance().build(RConfig.COMMUNITY_ACTIVITY_PUBLISH).navigation();
            }
        });

    }
}
